import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class CTPClient {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int PORT = 8088;

    public static void main(String[] args) {
        try (Socket socket = new Socket(SERVER_ADDRESS, PORT)) {
            System.out.println("Connected to server...");

            // 准备要发送的数据
            String data = "Sample CTP Data\n"
                    + "Timestamp: " + System.currentTimeMillis() + "\n"
                    + "Content: This is a test message from CTP client";
            
            // 发送数据
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
            
            // 先发送数据长度
            dos.writeInt(dataBytes.length);
            // 发送实际数据
            dos.write(dataBytes);
            dos.flush();

            // 接收服务器响应
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            String response = dis.readUTF();
            System.out.println("Server response: " + response);

        } catch (IOException e) {
            System.err.println("Client error: " + e.getMessage());
        }
    }
}